#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>

using namespace std;

class TopicSubscribe01 : public rclcpp::Node {
public:
  TopicSubscribe01(string name) : Node(name) {
    RCLCPP_INFO(this->get_logger(), "大家好，我是%s", name.c_str());
    // 创建一个订阅者订阅话题
    command_subscribe_ = this->create_subscription<std_msgs::msg::String>(
        "command", 10,
        bind(&TopicSubscribe01::command_callback, this, std::placeholders::_1));
  }

private:
  /* 声明订阅者 */
  rclcpp::Subscription<std_msgs::msg::String>::SharedPtr command_subscribe_;

  // 收到话题数据的回调函数
  void command_callback(const std_msgs::msg::String::SharedPtr msg) {
    double speed = 0.0f;
    if (msg->data == "forward") {
      speed = 0.2f;
    }
    RCLCPP_INFO(this->get_logger(), "收到[%s]指令，发送速度 %f",
                msg->data.c_str(), speed);
  }
};

int main(int argc, char **argv) {
  /* 初始化 */
  rclcpp::init(argc, argv);
  /* 创建节点 */
  auto node = make_shared<TopicSubscribe01>("topic_subscribe_01");
  /* 进入一个事件循环 */
  rclcpp::spin(node);
  /* 关闭节点 */
  rclcpp::shutdown();
  return 0;
}